from django.shortcuts import render

# Create your views here.
from .models import BookInfo, HeroInfo
from django.db.models import F, Q, Sum, Avg

"""ORM的新增操作"""

# save  create

# book = BookInfo()
# book.btitle = '小三国'
# book.bpub_date = '2019-11-13'
# book.save()

# book = BookInfo(
#     btitle='西游记后传',
#     bpub_date='1991-11-11'
# )
# book.save()

# book = BookInfo.objects.create(
#     btitle='西游记',
#     bpub_date='1986-11-11'
# )

# hero = HeroInfo(
#     hname='白骨精',
#     # hbook=book,  # 如果直接给模型中的外键赋值,必传赋当前外键代表的模型对象
#     hbook_id=book.id  # 如果给外键_id字段赋值,就必传赋当前外键代表模型对象的id值
# )
# hero.save()


"""删除"""

# book = BookInfo.objects.get(id=9)  # 获取id为9的那本书
# book.delete()  # 物理删除

# BookInfo.objects.filter(id__gt=2).delete()


"""基本查询
get()  查单一,如果查不到会报错
count()  获取要查询记录的条数
all()  查询所有

"""
# try:
#     book = BookInfo.objects.get(id=100)
# except BookInfo.DoesNotExist:

# book = BookInfo.objects.get(bread=12)
# print(book)
# print(BookInfo.objects.count())
# print(BookInfo.objects.all())



"""过滤查询
filter  过滤多个 可以查到一个多个或0个  返回QuerySet 查询集
exclude  查询满足条件以外的  返回 QuerySet
get  过滤单一
"""

# book = BookInfo.objects.get(id=1)
# book = BookInfo.objects.get(id__exact=1)
# book1 = BookInfo.objects.filter(id=1)[0]
# print(book, book1)

# book_qs = BookInfo.objects.filter(btitle__contains='湖')

# book_qs = BookInfo.objects.filter(btitle__endswith='部')

# book_qs = BookInfo.objects.filter(btitle__isnull=False)


# book_qs = BookInfo.objects.filter(id__in=[2, 4])
# book_qs = BookInfo.objects.filter(id__gt=2)
# book_qs = BookInfo.objects.exclude(id=3)
# book_qs = BookInfo.objects.filter(bpub_date__year='1980')
# book_qs = BookInfo.objects.filter(bpub_date__month='11')
# book_qs = BookInfo.objects.filter(bpub_date__gt='1990-01-01')

"""
F对象作用: 用来进行对当前对象的两个属性比较

"""
# book_qs = BookInfo.objects.filter(bread__gt=F('bcomment'))
# book_qs = BookInfo.objects.filter(bread__gt=F('bcomment') * 2)


"""
Q对象最主要作用就是使用多个条件查询的逻辑或  or
"""
# book_qs = BookInfo.objects.filter(id__gt=2, bread__gt=12)  # 此写法类似于and 逻辑与
# book_qs = BookInfo.objects.filter(Q(id__gt=2), Q(bread__gt=12))  # 此写法类似于and 逻辑与
# book_qs = BookInfo.objects.filter(id__lt=3, bread__gt=20)
# book_qs = BookInfo.objects.filter(Q(id__lt=3) | Q(bread__gt=20))  # or  逻辑或
# book_qs = BookInfo.objects.filter(~Q(id=3))
# book_qs = BookInfo.objects.exclude(id=3)
# book_qs = BookInfo.objects.filter(id=2)
# book_qs = BookInfo.objects.filter(Q(id=2))



"""
聚合函数
{'bread__sum': 296}
{'bread__avg': 59.2}
"""
# number = BookInfo.objects.aggregate(Avg('bread'))
# print(number)

"""
order_by('字段') 升序排序  
order_by('-字段') 降序排序  
"""

# book_qs = BookInfo.objects.order_by('bread')
# book_qs = BookInfo.objects.order_by('bread', 'bcomment')
# print(book_qs)

"""
关联查询
一查多:  book.heroinfo_set.all()  --> 一的模型对象.多方模型名小写_set.all() 
多查一:  hero.hbook ---> 多方模型对象.外键
"""
# book1 = BookInfo.objects.get(id=1)
# book1.heroinfo_set.all()
# book2 = BookInfo.objects.get(id=2)
# book2.heroinfo_set.all()

# book = BookInfo.objects.get(id=1)
# hero_qs = book.heroinfo_set.all()
# print(hero_qs)
# BookInfo.objects.get(id=1).heroinfo_set.all()

# hero = HeroInfo.objects.get(id=1)
# book = hero.hbook
# HeroInfo.objects.get(id=1).hbook
# print(book)

# class FatherModel:  一
#     sonmodel_set: 隐式字段,方便查询使用
#
# class SonModel: 多
#
#     father: 外键
#
# father = FatherModel.objects.get(id=1)
# father.sonmodel_set.all()

# son.father



"""
关联过滤查询
一查多: HeroInfo.objects.filter(hbook__btitle='天龙八部') 一作为过滤条件时: 外键__字段
多查一: BookInfo.objects.filter(heroinfo__hname='郭靖')  多为作过滤条件时: 多方模型名小写__字段
"""

# BookInfo.objects.filter(btitle='天龙八部')
# hero_qs = HeroInfo.objects.filter(hbook__btitle='天龙八部')
# hero_qs = HeroInfo.objects.filter(hbook__bread=12)
# print(hero_qs)

# book_qs = BookInfo.objects.filter(heroinfo__hname='郭靖')
# book_qs = BookInfo.objects.filter(heroinfo__hgender=0)
# print(book_qs)

# HeroInfo.objects.filter(hbook_id__in=[1, 2])
# HeroInfo.objects.filter(hbook__bread__gt=12)
# HeroInfo.objects.filter(id__gt=1).filter().filter().filter().order_by().order_by().all()


# qs = BookInfo.objects.all()
# print('-----')
# print(qs)

# [book.id for book in BookInfo.objects.all()]
# [book.id for book in BookInfo.objects.all()]

# qs=BookInfo.objects.all()
# [book.id for book in qs]
# [book.id for book in qs]


# qs = BookInfo.objects.all()[2]
# print(qs)

# BookInfo.objects.all()
# print(BookInfo.book_manager.all())


"""
修改: 
1.先把要修改的模型对象获取到,然后修改属性值,再save()
2.filter().update(字段名='xxx')
"""
# book = BookInfo.objects.get(id=5)
# book.btitle = '大三国'
# book.save()

BookInfo.objects.filter(id=5).update(bread=20)